<template>
	<view class="container">
		<view class="example">
			<uni-forms ref="form" :model="sfxx" label-width="100px" >
				<uni-forms-item label="随访标题" name="sfbt">
					<uni-easyinput :disabled="isDisabled" v-model="sfxx.sfbt"  @change="autoSaven" placeholder="请输入随访标题" />
				</uni-forms-item>
				<uni-forms-item label="随访时间" name="sfsj">
					<uni-datetime-picker type="date" :clear-icon="true" :disabled="isDisabled" v-model="sfxx.sfsj" />
				</uni-forms-item>
				<uni-forms-item label="随访结果" name="sfjg">
					<uni-easyinput :disabled="isDisabled" v-model="sfxx.sfjg"  @change="autoSaven" placeholder="请输入随访结果" />
				</uni-forms-item>
				<uni-forms-item label="使用药物" name="syyw">
					<uni-easyinput :disabled="isDisabled" v-model="sfxx.syyw"  @change="autoSaven" placeholder="请输入使用药物" />
				</uni-forms-item>
				<uni-forms-item label="副作用" name="fzy">
					<uni-easyinput :disabled="isDisabled" v-model="sfxx.fzy"  @change="autoSaven" placeholder="请输入副作用" />
				</uni-forms-item>
				<uni-forms-item label="效果" name="xg">
					<uni-easyinput :disabled="isDisabled" v-model="sfxx.xg"  @change="autoSaven" placeholder="请输入效果" />
				</uni-forms-item>
				<uni-forms-item label="是否更改方案" name="sfggfa">
					<uni-data-checkbox :disabled="isDisabled" v-model="sfxx.sfggfa" :localdata="dictSf" />
				</uni-forms-item>
				<uni-forms-item label="是否健在" name="sfjz">
					<uni-data-checkbox :disabled="isDisabled" v-model="sfxx.sfjz" :localdata="dictSf" />
				</uni-forms-item>
				<uni-forms-item label="特殊辅助检查" name="tsfzjc">
					<uni-easyinput :disabled="isDisabled" v-model="sfxx.tsfzjc"  @change="autoSaven" placeholder="请输入特殊辅助检查" />
				</uni-forms-item>
				<uni-forms-item label="备注" name="remark">
					<uni-easyinput type="textarea" autoHeight :disabled="isDisabled" v-model="sfxx.remark"  @change="autoSaven" placeholder="请输入备注"/>
				</uni-forms-item>
				<button type="primary" :disabled="isDisabled" @click="submit">保存</button>
			</uni-forms>
		</view>
	</view>
</template>

<script>
	import storage from '@/utils/storage'
import { addSfxx,updateSfxx,sfkybjData } from "@/api/trms/trms"
import { getSfxxInfo } from "@/api/trms/query"
export default {
	data() {
		return {
			eventChannel: null,
			isDisabled: false,
			userId: null,
			id: null,
			size: 1,
			// 表单数据
			sfxx: {},
			rules: {
				
			},
			dictSf: [
				{
				  text: '是',
				  value: 1
				}, {
				  text: '否',
				  value: 0
				}
			]
		}
	},
	onLoad: function(option) {
		console.log("option:",option)
		this.sfxx.patientId = option.patientId;
		this.id = option.id;
		this.size = option.size;
		this.userId = option.userId;
		let that = this;
		console.log(".....edit.....",this.id,this.patientId)
		this.eventChannel = this.getOpenerEventChannel();
		// 监听listSfxx事件，获取上一页面通过eventChannel传送到当前页面的数据
		this.eventChannel.on('listSfxx', function(data) {
			
		});
		sfkybjData(this);
		if(this.id){
			// 查询详情
			this.getDataById();
		}else{
			this.sfxx.sfbt = "第"+this.size+"次随访";
		}
	},
	onReady() {
		this.$refs.form.setRules(this.rules)
	},
	methods: {
		/**
		 * 获取数据
		 */
		getDataById(){
			getSfxxInfo(this.id).then(response => {
			  this.sfxx = response.data;
			});
		},
		/**
		 * 修改
		 */
		submit(){
			this.$refs.form.validate().then(res => {
				if(this.id){
					// 编辑
					updateSfxx(this.sfxx).then(response => {
					  if(response.code == 200){
						  this.$modal.msgSuccess("修改成功");
						  this.backWork();
						  return;
					  }
					  this.$modal.msgError(response.msg);
					});
				}else{
					// 新增
					addSfxx(this.sfxx).then(response => {
					  if(response.code == 200){
						  this.$modal.msgSuccess("新增成功");
						  this.backWork();
						  return;
					  }
					  this.$modal.msgError(response.msg);
					});
				}
			})
		},
		autoSaven() {
			let that = this;
			this.$refs.form.validate().then(res => {
				if(this.id){
					// 编辑
					updateSfxx(this.sfxx).then(response => {
					  console.log("修改成功");
					});
				}else{
					// 新增
					addSfxx(this.sfxx).then(response => {
					  if(response.code == 200){
						  that.id = response.data.id;
						  that.sfxx.id = response.data.id;
					  }
					  console.log("新增成功");
					});
				}
			})
		},
		/**
		 * 回到工作页面
		 */
		backWork(){
			let that = this;
			setTimeout(function (){
				// 向起始页通过事件传递数据
				that.eventChannel.emit('listSfxx', {data: 'refPage'});
				uni.navigateBack({
					delta: 1
				});
			},500);
		}
	}
}
</script>


<style lang="scss">
  page {
    background-color: #ffffff;
  }

  .example {
    padding: 15px;
    background-color: #fff;
  }

  .segmented-control {
    margin-bottom: 15px;
  }

  .button-group {
    margin-top: 15px;
    display: flex;
    justify-content: space-around;
  }

  .form-item {
    display: flex;
    align-items: center;
    flex: 1;
  }

  .button {
    display: flex;
    align-items: center;
    height: 35px;
    line-height: 35px;
    margin-left: 10px;
  }
</style>
